using System;
using System.Web.UI;
using System.Collections.Generic;
using Entity;
using eShop.Model;
using eShop.DataAccess;
using System.Web.Services;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using eShop.Business;
using System.Linq;
public partial class admin_default : SiteAdminBasePage
{
    

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static List<OrderReport> Report1()
    {
        List<OrderReport> _orders = ReportDAO.GetOrdersByMonth(DateTime.Now.AddMonths(-6), DateTime.Now);
        return _orders;
    }

    [WebMethod]
    public static String Report2()
    {
        List<ReportContainer> reports = ReportDAO.GetRecentOrderSummary();
        StringBuilder strJson = new StringBuilder();
        strJson.AppendLine("[");
        foreach (ReportContainer rep in reports)
        {
            strJson.AppendLine("[");
            strJson.AppendLine("'" + FormatOrderStatus(rep.key) + "'," + rep.Val);
            strJson.AppendLine("],");
        }

        String json = strJson.ToString();
        if (!String.IsNullOrEmpty(json) && strJson.Length>3)
        {
            json = json.Substring(0, json.LastIndexOf(","));
            json = json + "]";
            return json;
        }
        else
            return String.Empty;
        
    }

    [WebMethod]
    public static List<ReportContainer> Report3(string fromDate, string toDate)
    {
        List<ReportContainer> _orders = ReportDAO.GetUsersByMonth(Convert.ToDateTime(fromDate), Convert.ToDateTime(toDate));
        return _orders;
    }

    [WebMethod]
    public static List<OrderStatusReport> Report4(string fromDate, string toDate)
    {
        List<ReportByDate> reports = ReportDAO.GetOrderSummaryByMonth(Convert.ToDateTime(fromDate), Convert.ToDateTime(toDate));
        List<OrderStatusReport> _reports = new List<OrderStatusReport>();

        List<string> _categories = new List<string>();
        foreach(ReportByDate _dt in reports)
        {
            if (!_categories.Contains(_dt.DateSelected))
            {
                _categories.Add(_dt.DateSelected);
            }
        }

        foreach (string strVal in _categories)
        {
            OrderStatusReport _report = new OrderStatusReport();
            _report.DateSelected = strVal;
            ReportByDate _success=reports.FirstOrDefault(x => x.DateSelected.Equals(strVal) && x.key.Equals(OrderStatusType.CCSuccess.ToString()));
            if (_success != null)
            {
                _report.Success = Convert.ToInt32(_success.Val);
            }
            else
            {
                _report.Success = 0;

            }

            ReportByDate _fail = reports.FirstOrDefault(x => x.DateSelected.Equals(strVal) && x.key.Equals(OrderStatusType.CCFail.ToString()));
            if (_fail != null)
            {
                _report.Fail = Convert.ToInt32(_fail.Val);
            }
            else
            {
                _report.Fail = 0;

            }

            ReportByDate _manual = reports.FirstOrDefault(x => x.DateSelected.Equals(strVal) && x.key.Equals(OrderStatusType.ManualAuthorization.ToString()));
            if (_manual != null)
            {
                _report.Manual = Convert.ToInt32(_manual.Val);
            }
            else
            {
                _report.Manual = 0;

            }
            _reports.Add(_report);
        }
      
           
        return _reports;
    }


    private static string SerializeObjectIntoJson(ReportContainer p)
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType());
        using (MemoryStream ms = new MemoryStream())
        {
            serializer.WriteObject(ms, p);
            ms.Flush();
            byte[] bytes = ms.GetBuffer();
            string jsonString = Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim('\0');
            return jsonString;
        }

    }

    private static string FormatOrderStatus(string _status)
    {
        if (_status == OrderStatusType.CCFail.ToString())
            return "Fail";
        else if (_status == OrderStatusType.CCSuccess.ToString())
            return "Success";
        else
            return "Wait for approval";
    }
}
